import es
import random
import playerlib

import wcs

raceName = "Undead Scourge"
race = wcs.races[raceName]

""" Load the strings into the variable text """
text = race.loadStrings()

######################################
# Create Configs! (Must be done first)
config = race.config()
# Vampiric Aura
config.text("********************")
config.text("Vampiric Aura")
config.text("  Draw health from the damage that you inflict")
config.text("  to others.")
config.text("********************")
config.text("")
vampiricMaxLevel      = config.cvar("vampiricMaxLevel",      5, "The maximum level for the Vampiric Skill")
vampiricLevelInterval = config.cvar("vampiricLevelInterval", 2, "The intervals between the abilities to upgrade their Vampiric skill")
vampiricPercent       = config.cvar("vampiricPercent",      10, "This is the percent of health received in comparison to the damage, multiplied by the users level.")

# Unholy Aura
config.text("")
config.text("********************")
config.text("Unholy Aura")
config.text("  This skill Gives you a speed boost upon spawn")
config.text("********************")
config.text("")
unholyMaxLevel        = config.cvar("unholyMaxLevel",      4, "The maximum level for the Unholy Aura skill")
unholyLevelInterval   = config.cvar("unholyLevelInterval", 3, "The intervals between the abilities to upgrade their Unholy Aura skill")
unholyPercent         = config.cvar("unholyPercent",      10, "The speed increase percentage per level") 

# Levitation
config.text("")
config.text("********************")
config.text("Levitation")
config.text("  This skill Gives you a speed boost upon spawn")
config.text("********************")
config.text("")
levMaxLevel           = config.cvar("levMaxLevel",      5, "The maximum level for the Levitation skill")
levLevelInterval      = config.cvar("levLevelInterval", 1, "The intervals between the abilities to upgrade their Levitation skill")
levPercent            = config.cvar("levPercent",      15, "The gravity decrease percentage per level")

# Suicide Bomber
config.text("")
config.text("********************")
config.text("Suicide Bomber")
config.text("  This ultimate allows you to power up yourself to") 
config.text("  run in as a kamikaze, blow yourself up and take") 
config.text("  out others.")
config.text("********************")
config.text("")
bomberMaxLevel        = config.cvar("bomberMaxLevel",        4,  "The maximum level for the Suicide Bomber skill")
bomberMinLevel        = config.cvar("bomberMaxLevel",        8,  "The minimum level that this ultimate can be bought")
bomberLevelInterval   = config.cvar("bomberLevelInterval",   5,  "The interval between the skill upgrades")
bomberSuicideDamage   = config.cvar("bomberSuicideDamage",   10, "This is the amount of damage done multiplied by the power and the level")
bomberSuicideDistance = config.cvar("bomberSuicideDistance", 20, "This is the distance in game units (40 units = 1m) multiplied by the power and the level")

config.load(True)
# Load the file with server.cmd so it 
# loads instantly
########################################


race.registerSkill("Vampiric Aura", int(vampiricMaxLevel), int(vampiricLevelInterval), "Draw Health From the Damage you do to Others")
race.registerSkill("Unholy Aura",   int(unholyMaxLevel),   int(unholyLevelInterval),   "Speed Boost")
race.registerSkill("Levitation",    int(levMaxLevel),      int(levLevelInterval),      "Lower Gravity")

# Ultimate
race.registerUltimate("Suicide Bomber", int(bomberMinLevel), int(bomberMaxLevel), int(bomberLevelInterval), "Blow yourself up and damage nearbye enemies")

def player_spawn(event_var):
    """
    Executed when the player spawns. Ensure that we give them additional speed
    and lower their gravity dependant on their Unholy Aura and Leviation levels.
    """
    userid = event_var["userid"]
    player = wcs.players[userid]
    unholyLevel = player[raceName]["Unholy Aura"]
    if unholyLevel:
        plevel = unholyLevel * int(unholyPercent) * .01
        player.speedAdd(plevel)
        es.tell(userid, "#multi", text("unholy aura", {"speed": plevel * 100}, playerlib.getPlayer(userid).get("lang")))
    levLevel = player[raceName]["Levitation"]
    if levLevel:
        depth = 1 - levLevel * int(levPercent) * .01
        player.setGravity(depth)
        es.tell(userid,"#multi", text("levitation", {"gravity": depth * 100}, playerlib.getPlayer(userid).get("lang")))

def player_hurt(event_var):
    """
    Executed when a player is hurt. If the attacker has Vampiric Aura, then
    give them some life steal values.
    """
    attacker = int(event_var["attacker"]) if event_var["attacker"] else 0
    if not attacker:
        return
    if event_var["es_attackerteam"] == event_var["es_userteam"]:
        return
    userid   = event_var["userid"]
    dmg = wcs.getDamage(event_var)    
    wcsAttacker = wcs.players[attacker]
    vampiric = wcsAttacker[raceName]["Vampiric Aura"]
    if vampiric:
        #alpha = 60 + vampiric * 20
        #x,y,z = es.getplayerlocation(attacker)
        #xx,yy,zz = es.getplayerlocation(userid)
        #wcs.effect.BeamCustom("#a", "0", "sprites/lgtning.vmt", x, y, z + 20, xx, yy, zz + 20, 0.5, 40, 40, 0, random.randint(128, 255), random.randint(1, 127), alpha)
        amount = int(round(dmg * vampiric * (int(vampiricPercent) * .01)))
        wcsAttacker.Heal(amount)
        es.tell(attacker, "#multi", text("vampiric aura", {"health":amount, "player":es.getplayername(userid)}, playerlib.getPlayer(userid).get("lang")))
        
def player_ultimate_on(event_var):
    """
    Executed when the player presses down their ultimate button, ensure that
    we begin counting.
    """
    userid = event_var["userid"]
    player = wcs.players[userid]
    bomberLevel = player[raceName]["Suicide Bomber"]
    if bomberLevel:
        player["bombDamage"] = 0
        increaseDamage(userid)

def increaseDamage(userid):
    """
    Executed when we want to increase the damage counter of the suicide bomber.
    
    @param int userid The ID of the person currently charging their suicide bomb
    """
    player = wcs.players[userid]
    bombDamage = player["bombDamage"]
    if bombDamage < 20:
        player["bombDamage"] += 1
        player.hudHint("%s\n%s" % (text("power", lang=playerlib.getPlayer(userid).get("lang")), "|" * bombDamage))
        es.emitsound("player", userid, wcs.BLIP, 0.5, 0.5)
    else:
        player.hudHint("%s\n%s" % (text("power", lang=playerlib.getPlayer(userid).get("lang")), text("maxed", lang=playerlib.getPlayer(userid).get("lang"))))
    player.delayedName(0.5, "undead_%s" % userid, increaseDamage, userid)
    
def player_ultimate_off(event_var):
    """
    Executed when the player releases their ultimate, ensure that we get the
    player's current position, and create an area damage effect around the
    player's location.
    """
    userid = event_var["userid"]
    player = wcs.players[userid]
    player.cancelDelayed("undead_%s" % userid)
    bomberLevel = player[raceName]["Suicide Bomber"]
    if bomberLevel:
        damageval = player["bombDamage"]
        del player["bombDamage"]
        if not damageval:
            return
        team      = ("#t" if event_var["es_userteam"] == "3" else "#ct")
        distance  = bomberLevel     * int(bomberSuicideDistance) * (damageval /5.)
        damage    = int(bomberLevel * int(bomberSuicideDamage)   * (damageval / 5.))
        player.areaDamage(team, distance, damage, userid)
        sound = [
            "explode3.wav",
            "explode4.wav",
            "explode5.wav",
            "mortar/mortar_explode1.wav",
            "mortar/mortar_explode2.wav",
            "mortar/mortar_explode3.wav"
            ]
        sound = "weapons/%s" % random.choice(sound)
        #wcs.effect.Smoke(userid)
        es.emitsound("player", userid, sound, 1.0, damageval / 20.)
        player.damage(es.getplayerprop(userid, "CBasePlayer.m_iHealth"))
